package main

import (
	"fmt"
	"time"
)

func numbers() (int, int, string) {
	a, b, c := 1, 2, "a"
	return a, b, c
}

//const常量
func mian2(x, y int) int {
	const number int = 6
	const name string = "leshen"
	const nub, nub2, name2 = 1, 2, "glc"
	var area int
	area = number * nub
	fmt.Printf("面积为：%d\n", area)
	return area
}

//iota特殊变量
func aName() (int, int, int, string, int, int, int, int, int) {
	const (
		a = iota
		b
		c
		d = "aaa"
		e
		f = 100
		g
		h = iota
		i
	)
	fmt.Println(a, b, c, d, e, f, g, h, i)
	return aName()
}

//if else if else
func main2() int {
	const a, b = 2, 5
	a_number := 3
	var return_number int
	if a_number > a {
		fmt.Println(a_number)
		return_number = a_number
	} else if a_number > b {
		fmt.Println(b)
		return_number = b
	} else if a > b {
		fmt.Println(a)
		return_number = a
	} else {
		fmt.Println(b)
		return_number = b
	}

	return return_number
}

//if嵌套
func ifElse() int {
	var a int
	a_number := 1
	fmt.Printf("请输入密码：")
	fmt.Scan(&a)
	if a_number == 1 {
		if a_number == a {
			fmt.Println("输入正确")
		} else {
			fmt.Println("输入错误")
		}
	}
	fmt.Println(a)
	return a
}

//switch
func switchTest() string {
	grade := "A"
	marks := 90
	switch marks {
	case 90:
		grade = "A"
	case 60:
		grade = "B"
	default:
		grade = "C"

	}
	fmt.Println(grade)
	return grade
}

//for
func forTest() {
	sum := 0
	for j := 0; j <= 10; j++ {
		sum += j
	}
	fmt.Println(sum)
	strings := []string{"a", "x", "aaa"}
	for _, s := range strings {
		fmt.Println(s)
	}
	numbers := [5]int{1, 2, 4}
	for _, s := range numbers {
		fmt.Println(s)
	}
}

func forRange() {
	for i := 1; i < 10; i++ {
		for j := 1; j < i; j++ {
			fmt.Printf("%d * %d = %d ", i, j, i*j)
		}
		fmt.Println("")
	}
}

func aaa() {
	aNumber := func(x int) int {
		return x * 2
	}
	fmt.Println(aNumber(9))
}

//数组,数组当参数传递到函数中
//func aList(oneList [][]int, aNumber int) {
//	//var aTestList [...]int
//	//print(aTestList)
//	aLis := [3]int{1, 2, 3}
//	print(aLis)
//
//	//var aTestList2 [...][...]int
//	//print(aTestList2)
//	aLis2 := [2][3]int{{1, 2, 3}, {1, 2, 3}}
//	print(aLis2)
//}

//指针
func aZhiZhen() {
	a := 10
	var ip *int
	ip = &a
	fmt.Printf("a的变量地址是：%x", &a)
	fmt.Printf("ip变量的储存地址是：%x", ip)
	fmt.Println("ip变量的值是:", *ip)
	const MAX = 3
	b := [3]int{1, 2, 3}
	var ptr [MAX]*int
	for i := 0; i < MAX; i++ {
		ptr[i] = &b[i]
	}
	fmt.Println(ptr)
	for _, s := range ptr {
		fmt.Println(*s)
	}
}

//结构体
type aStruct struct {
	title   string
	author  string
	subject string
	id      int
}

func aStructTest() {
	aTestDict := aStruct{"计算帝国", "乐神", "computer", 12345}
	fmt.Println(aTestDict)
	fmt.Println(aTestDict.author)
	a := aStructTest2(aTestDict)
	fmt.Println(a)
	b := aStructTest3(&aTestDict) //需要讲物理地址符号传进去：&
	fmt.Println(b)
}
func aStructTest2(book aStruct) int {
	fmt.Println(book.id)
	return book.id
}
func aStructTest3(book *aStruct) string {
	fmt.Println(book.title)
	return book.title
}

//切片
func makeTest() {
	aList := []int{1, 2, 3, 4, 5}
	fmt.Println(aList)
	fmt.Println(aList[:3])
	fmt.Println(aList[1:3])
	fmt.Println(len(aList))
	fmt.Println(cap(aList))
	//fmt.Println(aList[9:]) //错误的
	//fmt.Println(aList[:9]) //错误的
	newList := []int{11, 22, 33}
	aList = append(aList, 90)
	fmt.Println(aList)

	var copyList []int

	copy(copyList, newList)
	fmt.Println(copyList)
}

//集合 map
func aMap() {
	aTestMap := map[string]string{"name": "guolc", "age": "2", "hobby": "angry"}
	fmt.Println(aTestMap)
	fmt.Println(aTestMap["name"])
	capital, ok := aTestMap["aaa"]
	if ok {
		fmt.Println(capital)
	} else {
		fmt.Println(ok, capital, "aaa")
	}
	delete(aTestMap, "hobby")
	fmt.Println(aTestMap)
	aTestMap["like"] = "sajiao"
	fmt.Println(aTestMap)
}

//递归函数/阶乘
func aRecursion(aNumber int) (result int) {
	if aNumber > 0 {
		result = aNumber * aRecursion(aNumber-1)
		return result
	}
	return 1
}

//转换类型
func exchangeType() {
	a := 12.56
	var b float32
	var c int
	b = float32(a)
	c = int(a)
	fmt.Println(c)
	fmt.Println(b)
}

//定义接口：首先定义接口，然后定义结构体，最后直接调用接口中的方法
type testInterface interface {
	call(a string)
}
type testStruct struct {
	name string
	age  int
}

func (a_Struct testStruct) call(a string) {
	fmt.Println("我是一个call函数", a)
}

//错误处理
type errorStruct struct {
	number1 int
	number2 int
}

func (es errorStruct) Error() string {
	strFormat := `
	IS ZERO
	number1:%d
	number2:0
`
	return fmt.Sprintf(strFormat, es.number1)
}
func errorFunc(n1 int, n2 int) (result int, errMsg string) {
	if n2 == 0 {
		es2 := errorStruct{n1, n2}
		errMsg = es2.Error()
		return
	} else {
		return n1 / n2, ""
	}
}

//并发
func tongBu(aStr int) {
	for i := 1; i < 5; i++ {
		time.Sleep(100 * time.Millisecond)
		fmt.Println(aStr)
	}
}

// 通道缓冲
func aChannel() {
	ch := make(chan string, 2)
	ch <- "aaaaaaa"
	ch <- "bbbbbbbbbb"
	fmt.Println(<-ch)
	fmt.Println(ch) //仅获取到地址
}

//遍历通道数据和关闭通道
func fibonacci(n int, ch chan int) {
	x, y := 0, 1
	for i := 1; i < n; i++ {
		ch <- x
		x, y = y, x+y
	}
	close(ch)
}
func main() {
	ch := make(chan int, 5)
	go fibonacci(cap(ch), ch)
	for i := range ch {
		fmt.Println(i)
	}
	//aChannel()
	//go tongBu(3)
	//tongBu(66)

	a_format, errMsg := errorFunc(100, 0)
	if errMsg == "" {
		fmt.Println("我是存在的", a_format)
	} else {
		fmt.Println("我是不存在的", a_format, errMsg)
	}
	a := testStruct{"glc", 2}
	a.call("叫爸爸")
	//输出文件
	fmt.Println("kkkk")
	var name string
	name = "S"
	fmt.Println(name)
	f := 1
	fmt.Println(f)

	var a_false bool
	a_false = true
	fmt.Println(a_false)
	_, _, c_c := numbers()
	fmt.Println(c_c)
	_, numb, strs := numbers() //只获取函数返回值的后两个
	fmt.Println(numb, strs)
	exchangeType()
}
